home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / NextAnswers / 1601_nilist.c < prev    next >
Text File  |  1995-11-28  |  3KB  |  113 lines

  1. /*
  2.  * nilist
  3.  *
  4.  * usage: nilist domain directory
  5.  * compile with: cc -o nilist nilist.c
  6.  *
  7.  * Similar to "niutil -list domain directory [key]"
  8.  *
  9.  * Sample output:
  10.  *
  11.  *    myhost> nilist . /users 
  12.  *    25       "root"
  13.  *    27       "nobody"
  14.  *    29       "agent"
  15.  *    31       "daemon"
  16.  *    33       "uucp"
  17.  *    35       "news"
  18.  *    37       "sybase"
  19.  *    39       "me"
  20.  *    myhost> nilist . /users uid
  21.  *    25       "0"
  22.  *    27       "-2"
  23.  *    29       "1"
  24.  *    31       "1"
  25.  *    33       "4"
  26.  *    35       "6"
  27.  *    37       "8"
  28.  *    39       "20"
  29.  *
  30.  * Author: Marc Majka
  31.  * You may freely copy, distribute and reuse the code in this example.  
  32.  * NeXT disclaims any warranty of any kind, expressed or implied, as to 
  33.  * its fitness for any particular use.
  34.  */
  35.  
  36. #import <ansi/stdio.h>
  37. #import <bsd/errno.h>
  38. #import <netinfo/ni.h>
  39.  
  40. main(int argc, char *argv[]) {
  41.  
  42.     int             en, vn;
  43.     ni_entrylist    elist;
  44.     ni_entry        entry;
  45.     ni_namelist     values;
  46.     void            *dom;
  47.     ni_id           dir;    
  48.     ni_status       ret;
  49.     char            listkey[256];
  50.  
  51.     /* check usage */
  52.     if (argc < 3) {
  53.         fprintf(stderr, "usage: %s domain directory [key]\n", argv[0]);
  54.         exit(1);
  55.     }
  56.  
  57.     /* argv[1] should be a domain name */
  58.     ret = ni_open(NULL, argv[1], &dom);
  59.     if (ret != NI_OK) {
  60.         fprintf(stderr, "ni_open: %s\n", ni_error(ret));
  61.         exit(1);
  62.     }
  63.  
  64.     /* argv[2] should be a directory specification */
  65.     ret = ni_pathsearch(dom, &dir, argv[2]);
  66.     if (ret != NI_OK) {
  67.         fprintf(stderr, "ni_pathsearch: %s\n", ni_error(ret));
  68.         exit(1);
  69.     }
  70.  
  71.     /* if there's a third argument, it's a property key */
  72.     /* by default, we'll list directories by name */
  73.     if (argc > 3) {
  74.         strcpy(listkey, argv[3]);
  75.     }
  76.     else {
  77.         strcpy(listkey, "name");
  78.     }
  79.  
  80.     /* get the values of the specified key for all subdirectories */
  81.     ret = ni_list(dom, &dir, listkey, &elist);
  82.     if (ret != NI_OK) {
  83.         fprintf(stderr, "ni_list: %s\n", ni_error(ret));
  84.         exit(1);
  85.     }
  86.  
  87.     /* for each entry */
  88.     for (en = 0; en <  elist.ni_entrylist_len; en++) {
  89.  
  90.         entry = elist.ni_entrylist_val[en];
  91.  
  92.         /* print the directory ID */
  93.         printf("%ld\t", entry.id);
  94.  
  95.         /* entries contain a pointer to a namelist of values */
  96.         /* if there are no values for the key, the pointer is null */
  97.         if (entry.names != NULL) {
  98.             values = *entry.names;
  99.  
  100.             /* for each value in the namelist for this property */
  101.             for (vn = 0; vn < values.ni_namelist_len; vn++) {
  102.                 /* print the value enclosed in quotes */
  103.                 printf(" \"%s\"", values.ni_namelist_val[vn]);
  104.             }
  105.         }
  106.         printf("\n");
  107.     }
  108.  
  109.     /* clean up */
  110.     ni_free(dom);
  111.  
  112.     exit(0);
  113. }